<template>
  <div class="mod-poap__poapprojectcode">
    <el-drawer v-model="state.drawerCode" title="源代码" size="90%" direction="rtl" :before-close="handleClose">
      <el-form :inline="true" :model="state.dataForm" @keyup.enter="state.getDataList()">
        <!--      <el-form-item>-->
        <!--        <el-input v-model="state.dataForm.projectId" placeholder="项目id" clearable></el-input>-->
        <!--      </el-form-item>-->
        <!--      <el-form-item>-->
        <!--        <el-input v-model="state.dataForm.codeId" placeholder="数据集id" clearable></el-input>-->
        <!--      </el-form-item>-->
        <!--      <el-form-item>-->
        <!--        <el-button @click="state.getDataList()">{{ $t("query") }}</el-button>-->
        <!--      </el-form-item>-->
        <el-form-item>
          <el-button type="info" @click="state.exportHandle()">{{ $t("export") }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-if="state.hasPermission('poap:poapprojectcode:save')" type="primary"
            @click="addOrUpdateHandle()">{{ $t("add") }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-if="state.hasPermission('poap:poapprojectcode:delete')" type="danger"
            @click="state.deleteHandle()">{{ $t("deleteBatch") }}</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="state.dataListLoading" :data="state.dataList" border
        @selection-change="state.dataListSelectionChangeHandle" style="width: 100%">
        <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
        <el-table-column prop="projectId" label="项目id" header-align="center" align="center"></el-table-column>
        <el-table-column prop="codeId" label="数据集id" header-align="center" align="center"></el-table-column>
        <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
          <template v-slot="scope">
            <el-button v-if="state.hasPermission('poap:poapprojectcode:update')" type="primary" link
              @click="addOrUpdateHandle(scope.row.id)">{{ $t("update") }}</el-button>
            <el-button v-if="state.hasPermission('poap:poapprojectcode:delete')" type="primary" link
              @click="state.deleteHandle(scope.row.id)">{{ $t("delete") }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination :current-page="state.page" :page-sizes="[10, 20, 50, 100]" :page-size="state.limit"
        :total="state.total" layout="total, sizes, prev, pager, next, jumper" @size-change="state.pageSizeChangeHandle"
        @current-change="state.pageCurrentChangeHandle"> </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update :key="addKey" ref="addOrUpdateRef" @refreshDataList="state.getDataList"></add-or-update>
    </el-drawer>
  </div>
</template>

<script lang="ts" setup>
import useView from "@/hooks/useView";
import { nextTick, reactive, ref, toRefs, watch } from "vue";
import AddOrUpdate from "./poapprojectcode-add-or-update.vue";
import { ElMessageBox } from "element-plus";

const props = defineProps({
  projectId: String,
  projectStatus: Number
});
const view = reactive({
  getDataListURL: "/poap/poapprojectcode/page",
  getDataListIsPage: true,
  exportURL: "/poap/poapprojectcode/export",
  deleteURL: "/poap/poapprojectcode",
  deleteIsBatch: true,
  drawerCode: false,
  dataForm: {
    projectId: "" as any,
    codeId: ""
  }
});

const state = reactive({ ...useView(view), ...toRefs(view) });

const addKey = ref(0);
const addOrUpdateRef = ref();
const addOrUpdateHandle = (id?: number) => {
  addKey.value++;
  nextTick(() => {
    addOrUpdateRef.value.init(id);
  });
};

const initCode = () => {
  state.drawerCode = true;
  state.dataForm.projectId = props.projectId;
  // if (props.projectStatus === 1 || props.projectStatus === 2) this.buttonShow = false;
  refresh();
};
const refresh = () => {
  state.getDataList();
};
const handleClose = () => {
  ElMessageBox.confirm("确定要关闭吗？", { confirmButtonText: "确认", cancelButtonText: "取消" }).then(() => {
    state.drawerCode = false;
  });
};

defineExpose({ initCode });
</script>
